const lodash = require('lodash')
const findDeep = require('deepdash/getFindDeep')(lodash)
const treeData = require('./treeData')


// const searchValue = 3
// const nodeMeta = findDeep(treeData, (node, key, parent, context) => {
//   if (node.value === searchValue) return true
// }, {
//   childrenPath: ['children']
// })

const defaultItemPropMap = {
  keyProp: 'value',
  deepdashOptions: {
    childrenPath: ['children']
  }
}


function findAncestors(
  treeData,
  node,
  propMap = defaultItemPropMap
) {
  const searchValue = node[propMap.keyProp]
  const nodeMeta = findDeep(
    treeData,
    node => {
      if (node.value === searchValue) return true
    },
    propMap.deepdashOptions
  )

  return nodeMeta ? nodeMeta.context.parents.map(item => item.value) : []
}

module.exports = findAncestors

// const node = {
//   value: 3
// }

// const parents = findAncestors(treeData, node, defaultItemPropMap)

// console.log(`parents of node value ${node.value}: `, parents)

// const levelPaths = parents.map(item => item.value.name).concat(nodeMeta.value.name)

// console.log(`level paths of node value ${searchValue}: `, levelPaths)

// console.log(`level paths of node value ${searchValue}: `, levelPaths.join('/'))
